package com.hykj.xxltest.contral;

import com.aliyun.oss.OSSClient;
import com.hykj.xxltest.base.AppBack;
import com.hykj.xxltest.config.Config;
import com.hykj.xxltest.utils.Base64Util;
import com.hykj.xxltest.utils.DeleteFileUtil;
import com.hykj.xxltest.utils.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 文件上传
 *
 * @author fishlord
 */
@Controller
@Api(description = "文件上传")
public class FileController {

    private static final Logger logger = LoggerFactory.getLogger(FileController.class);


    public static final int MaxSize = 50 * 1024 * 1024;//上传标文最大大小

    public static final String[] AllowType = {"txt", "doc"};//允许的标文类型

    /**
     * 上传图片
     */
    @RequestMapping("/file/uploadimg")
    @ResponseBody
    @ApiOperation(
            value = "上传图片",
            notes = "备注",
            httpMethod = "POST"
    )
    public AppBack uploadimg(@RequestParam("file") MultipartFile file) throws Exception {
        //TODO
        String fileName = file.getOriginalFilename();
        logger.info(fileName + "_" + file.getOriginalFilename() + "_" + file.getContentType());
        String ext = fileName.substring(fileName.indexOf("."), fileName.length());
        Random random = new Random();
        fileName = "" + random.nextInt(10000) + System.currentTimeMillis() + ext;
        File newFlie = new File(Config.FilePath + file.getOriginalFilename());
        file.transferTo(newFlie);
        try {
            Thumbnails.of(newFlie).scale(1f).outputQuality(0.25f).toFile(Config.FilePath + "s_" + fileName);
            newFlie.delete();
            return new AppBack("/files/" + "s_" + fileName);
        } catch (Exception e) {
            return new AppBack("/files/" + fileName);
        }


//        ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes());
//        OSSClient client = OSSUtil.getOSSClient();
//        String pictrue = OSSUtil.uploadBytes2OSS(client, OSSUtil.FILE, OSSUtil.BUCKETNAME, bais, file.getOriginalFilename());
//        return new AppBack(pictrue);
    }

    /**
     * 上传文件
     *
     * @return
     */
    @RequestMapping("/file/upload")
    @ResponseBody
    @ApiOperation(
            value = "上传文件",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack upload(@RequestParam("file") MultipartFile file) throws Exception {
        //TODO

        String fileName = file.getOriginalFilename();
        logger.info(fileName + "_" + file.getOriginalFilename() + "_" + file.getContentType());
        String ext = fileName.substring(fileName.indexOf("."), fileName.length());
        Random random = new Random();
        fileName = "" + random.nextInt(10000) + System.currentTimeMillis() + ext;
        File newFlie = new File(Config.FilePath + fileName);
        file.transferTo(newFlie);
        return new AppBack(fileName);
//        try {
//            Thumbnails.of(newFlie).scale(1f).outputQuality(0.25f).toFile(Config.FilePath + "s_" + fileName);
//            newFlie.delete();
//            return new AppBack("/files/" + "s_" + fileName);
//        } catch (Exception e) {
//            return new AppBack("/files/" + fileName);
//        }


//        ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes());
//        OSSClient client = OSSUtil.getOSSClient();
//        String pictrue = OSSUtil.uploadBytes2OSS(client, OSSUtil.FILE, OSSUtil.BUCKETNAME, bais, file.getOriginalFilename());
//        return new AppBack(pictrue);

    }

    /**
     * 批量上传
     *
     * @return
     */
    @RequestMapping("/file/mulUpload")
    @ResponseBody
    @ApiOperation(
            value = "批量上传",
            notes = "备注：",
            httpMethod = "POST")
    public Map mulUpload(StandardMultipartHttpServletRequest request) throws IOException {
        List<String> list = new ArrayList<>();
        Map<String, MultipartFile> fileMaps = request.getFileMap();//图片列表
        for (MultipartFile file : fileMaps.values()) {
//            ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes());
//            OSSClient client = OSSUtil.getOSSClient();
//            String pictrue = OSSUtil.uploadBytes2OSS(client, OSSUtil.FILE, OSSUtil.BUCKETNAME, bais, file.getOriginalFilename());
//            list.add(pictrue);
            String fileName = file.getOriginalFilename();
            logger.info(fileName + "_" + file.getOriginalFilename() + "_" + file.getContentType());
            String ext = fileName.substring(fileName.indexOf("."), fileName.length());
            Random random = new Random();
            fileName = "" + random.nextInt(10000) + System.currentTimeMillis() + ext;
            File newFlie = new File(Config.FilePath + file.getOriginalFilename());
            file.transferTo(newFlie);
            Thumbnails.of(newFlie).scale(1f).outputQuality(0.25f).toFile(Config.FilePath + "s_" + fileName);
            newFlie.delete();
            String pictrue = "/files/" + "s_" + fileName;
            list.add(pictrue);
        }
        Map map = new HashMap();
        map.put("errno", 0);
        map.put("data", list);
        return map;
    }


    /**
     * 上传
     *
     * @return
     */
    @RequestMapping("/file/uploadFile")
    @ResponseBody
    @ApiOperation(
            value = "上传任意文件",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(file.getBytes());
        OSSClient client = OSSUtil.getOSSClient();
        String pictrue = OSSUtil.uploadBytes2OSS(client, OSSUtil.FILE2, OSSUtil.BUCKETNAME, bais, file.getOriginalFilename());

        return new AppBack(pictrue);


    }


    public static void main(String[] args) {
        String[] arr = {"1", "2", "3", "4", "6", "7"};
        List<String> list = Arrays.asList(FileController.AllowType);
        System.out.println(list.contains("txt"));
    }

    /**
     * 上传
     *
     * @return
     */
    @RequestMapping("/file/base64")
    @ResponseBody
    @ApiOperation(
            value = "上传图片",
            notes = "备注：",
            httpMethod = "POST")
    public AppBack upload(@RequestParam String base64) {
        //TODO
        byte[] bytes = Base64Util.decode(base64);
        logger.info(bytes.length + "  ");
        Random random = new Random();
        String fileName = "" + random.nextInt(10000) + System.currentTimeMillis() + ".jpg";
        DeleteFileUtil.addFile(bytes, Config.FilePath + fileName);
        return new AppBack("/files/" + fileName);

    }


}
